package com.beauty.dressing.module.mine.persenter;


import android.util.Log;

import com.beauty.dressing.common.base.BasePresenter;
import com.beauty.dressing.common.bean.AddressBean;
import com.beauty.dressing.common.bean.AddressManagerBean;
import com.beauty.dressing.common.bean.BaseObserver;
import com.beauty.dressing.common.bean.BaseParse;
import com.beauty.dressing.module.mine.persenter.contract.AddressMangerContract;
import com.beauty.dressing.rxandroid.api.ApiManager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class AddressManagerPresenter extends BasePresenter implements AddressMangerContract.Presenter {
    private static final String TAG = "AddressManagerPresenter";
    private AddressMangerContract.ViewControl mViewControl;

    public AddressManagerPresenter(AddressMangerContract.ViewControl mViewControl) {
        this.mViewControl = mViewControl;
    }

    @Override
    public void getAllAddress(long member_id, String token) {
        mViewControl.showProgress();
        Map<String, String> map = new HashMap<>();
        map.put("member_id", String.valueOf(member_id));
        map.put("token", token);
        ApiManager.getInstence().getAddressService().allAddress(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<Object, AddressManagerBean>() {
                    @Override
                    protected void onSuccees(String message, Object data, List<AddressManagerBean> list, BaseParse extra) {
                        Log.e(TAG, extra.toString());
                        mViewControl.onGetAllAddress(true, 200, message, list);
                    }

                    @Override
                    protected void onFailed(int code, String message, Object data, List<AddressManagerBean> list, BaseParse extra) {

                    }
                });
    }

    @Override
    public void updateAddress(long member_id, String token, String address_id, String area, String detailed, String name, String phone, String status) {
        mViewControl.showProgress();
        Map<String, String> map = new HashMap<>();
        map.put("member_id", String.valueOf(member_id));
        map.put("token", token);
        map.put("address_id", address_id);
        map.put("area", area);
        map.put("detailed", detailed);
        map.put("name", name);
        map.put("phone", phone);
        map.put("status", status);
        ApiManager.getInstence().getAddressService().updateAddress(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<Object, Object>() {
                    @Override
                    protected void onSuccees(String message, Object data, List<Object> list, BaseParse extra) {
                        mViewControl.onUpdateAddress(true, 200, message);
                    }

                    @Override
                    protected void onFailed(int code, String message, Object data, List<Object> list, BaseParse extra) {
                        mViewControl.onUpdateAddress(true, 200, message);
                    }
                });
    }

    @Override
    public void createAddress(long member_id, String token, String area, String detailed, String name, String phone, String status) {
        mViewControl.showProgress();
        Map<String, String> map = new HashMap<>();
        map.put("member_id", String.valueOf(member_id));
        map.put("token", token);
        map.put("area", area);
        map.put("detailed", detailed);
        map.put("name", name);
        map.put("phone", phone);
        map.put("status", status);

        ApiManager.getInstence().getAddressService().createAddress(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<Object, Object>() {
                    @Override
                    protected void onSuccees(String message, Object data, List<Object> list, BaseParse extra) {
                        mViewControl.onCreateAddress(true, 200, message);
                    }

                    @Override
                    protected void onFailed(int code, String message, Object data, List<Object> list, BaseParse extra) {
                        mViewControl.onCreateAddress(false, code, message);
                    }
                });
    }

    @Override
    public void areas(String area_id, String token) {
        mViewControl.showProgress();
        Map<String, String> map = new HashMap<>();
        map.put("area_id", area_id);
        map.put("token", token);
        ApiManager.getInstence().getAddressService().areas(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<Object, AddressBean>() {
                    @Override
                    protected void onSuccees(String message, Object data, List<AddressBean> list, BaseParse extra) {
                        mViewControl.onAreas(true, 200, message, list);
                    }

                    @Override
                    protected void onFailed(int code, String message, Object data, List<AddressBean> list, BaseParse extra) {
                        mViewControl.onAreas(false, code, message, list);
                    }
                });
    }
}
